<?php

class Skjb_Office_Spreadsheet_Exporter_Excel2007Xml_Styles
{
    protected $_fonts = array(
        array(
            'name' => 'Calibri',
            'family' => 2,
            'scheme' => 'minor',
            'size' => 11,
            'color' => 1,
        ),
    );
    
    protected $_fills = array(
        'none',
        'gray125',
    );
    
    protected $_borders = array(
        array(
            'left',
            'right',
            'top',
            'bottom',
            'diagonal',
        ),
    );
    
    protected $_cellStyleXfs = array(
        array(
            'numFmtId' => 0,
            'fontId' => 0,
            'fillId' => 0,
            'borderId' => 0,
        ),
    );
    
    protected $_cellXfs = array(
        array(
            'numFmtId' => 0,
            'fontId' => 0,
            'fillId' => 0,
            'borderId' => 0,
            'xfId' => 0,
        ),
    );
    
    protected $_cellStyles = array(
        array(
            'name' => 'Normal',
            'xfId' => 0,
            'builtinId' => 0,
        ),
    );
    
    protected $_dxfs = array();
    
    public function toXml()
    {
        $xml = new XMLWriter();
        $xml->openMemory();
        $xml->startDocument('1.0', 'UTF-8', 'yes');
        
        $xml->startElement('styleSheet');
        $xml->writeAttribute('xmlns', 'http://schemas.openxmlformats.org/spreadsheetml/2006/main');
        
        $xml->startElement('fonts');
        $xml->writeAttribute('count', count($this->_fonts));
        
        foreach ($this->_fonts as $font) {
            $xml->startElement('font');
            
            $xml->startElement('sz');
            $xml->writeAttribute('val', $font['size']);
            $xml->endElement(); // sz
            
            $xml->startElement('color');
            $xml->writeAttribute('theme', $font['theme']);
            $xml->endElement(); // color
            
            $xml->startElement('name');
            $xml->writeAttribute('val', $font['name']);
            $xml->endElement(); // name
            
            $xml->startElement('family');
            $xml->writeAttribute('val', $font['family']);
            $xml->endElement(); // family
            
            $xml->startElement('scheme');
            $xml->writeAttribute('val', $font['scheme']);
            $xml->endElement(); // scheme
            
            $xml->endElement(); // font
        }
        
        $xml->endElement(); // fonts
        
        $xml->startElement('fills');
        $xml->writeAttribute('count', count($this->_fills));
        
        foreach ($this->_fills as $fill) {
            $xml->startElement('fill');
            
            $xml->startElement('patternFill');
            $xml->writeAttribute('patternType', $fill);
            $xml->endElement(); // patternFill
            
            $xml->endElement(); // fill
        }
        
        $xml->endElement(); // fills
        
        $xml->startElement('borders');
        $xml->writeAttribute('count', count($this->_borders));
        
        foreach ($this->_borders as $border) {
            $xml->startElement('border');
            $xml->writeElement('left');
            $xml->writeElement('right');
            $xml->writeElement('top');
            $xml->writeElement('bottom');
            $xml->writeElement('diagonal');
            $xml->endElement(); // border
        }
        
        $xml->endElement(); // borders
        
        $xml->startElement('cellStyleXfs');
        $xml->writeAttribute('count', count($this->_cellStyleXfs));
        
        foreach ($this->_cellStyleXfs as $cellStyleXf) {
            $xml->startElement('xf');
            $xml->writeAttribute('numFmtId', $cellStyleXf['numFmtId']);
            $xml->writeAttribute('fontId', $cellStyleXf['fontId']);
            $xml->writeAttribute('fillId', $cellStyleXf['fillId']);
            $xml->writeAttribute('borderId', $cellStyleXf['borderId']);
            $xml->endElement(); // xf
        }
        
        $xml->endElement(); // cellStyleXfs
        
        $xml->startElement('cellXfs');
        $xml->writeAttribute('count', count($this->_cellXfs));
        
        foreach ($this->_cellXfs as $cellXf) {
            $xml->startElement('xf');
            $xml->writeAttribute('numFmtId', $cellXf['numFmtId']);
            $xml->writeAttribute('fontId', $cellXf['fontId']);
            $xml->writeAttribute('fillId', $cellXf['fillId']);
            $xml->writeAttribute('borderId', $cellXf['borderId']);
            $xml->writeAttribute('xfId', $cellXf['xfId']);
            $xml->endElement(); // xf
        }
        
        $xml->endElement(); // cellXfs
        
        $xml->startElement('cellStyles');
        $xml->writeAttribute('count', count($this->_cellStyles));
        
        foreach ($this->_cellStyles as $cellStyle) {
            $xml->startElement('cellStyle');
            $xml->writeAttribute('name', $cellStyle['name']);
            $xml->writeAttribute('xfId', $cellStyle['xfId']);
            $xml->writeAttribute('builtinId', $cellStyle['builtinId']);
            $xml->endElement(); // cellStyle
        }
        
        $xml->endElement(); // cellStyles
        
        $xml->startElement('dxfs');
        $xml->writeAttribute('count', count($this->_dxfs));
        $xml->endElement(); // dxfs
        
        $xml->startElement('tableStyles');
        $xml->writeAttribute('count', '0');
        $xml->writeAttribute('defaultTableStyle', 'TableStyleMedium9');
        $xml->writeAttribute('defaultPivotStyle', 'PivotStyleLight16');
        $xml->endElement(); // tableStyles
        
        $xml->endElement(); // styleSheet
        
        $xml->endDocument();
        
        return $xml->outputMemory();
    }
}
